#include "ticketmoneyadjustwidget.h"
#include "ui_ticketmoneyadjustwidget.h"

TicketMoneyAdjustWidget::TicketMoneyAdjustWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TicketMoneyAdjustWidget)
{
    ui->setupUi(this);
    this->relation = nullptr;
    this->message = nullptr;

    setWindowTitle("票款调整");

    connect(&userRelationService, &UserRelationService::adjustUserRelationFinanceSuccess,
            [=]()
    {
        this->close();
    });
}

TicketMoneyAdjustWidget::~TicketMoneyAdjustWidget()
{
    delete ui;
}

void TicketMoneyAdjustWidget::showWithMessage(Message *message){
    this->message = message;
    this->relation = message->getUserRelation();

    ui->lineEditNickName->setText(relation==nullptr?"":relation->nickName);
    ui->lineEditContact->setText(relation==nullptr?"":relation->contact);
    ui->lineEditTel->setText(relation==nullptr?"":relation->tel);

    qDebug()<<"show2";
    ticketIn = message==nullptr?0:message->ticketOut;
    ticketOut = message==nullptr?0:message->ticketIn;
    moneyIn = message==nullptr?0:message->moneyOut;
    moneyOut = message==nullptr?0:message->moneyIn;
    double financeValue = 0;
    ticketValue = 0;
    moneyValue = 0;
    if(ticketOut != 0.0){
        financeValue = ticketOut;
        ticketValue = ticketOut;
        ui->labelFinanceValue->setText("应开发票：");
    }else if (moneyIn != 0.0) {
        financeValue = moneyIn;
        moneyValue = moneyIn;
        ui->labelFinanceValue->setText("应收货款：");
    }else if(ticketIn != 0.0){
        financeValue = ticketIn;
        ticketValue = ticketIn;
        ui->labelFinanceValue->setText("应收发票：");
    }else if(moneyOut != 0.0){
        financeValue = moneyOut;
        ticketValue = moneyOut;
        ui->labelFinanceValue->setText("应付货款：");
    }
    qDebug()<<"show1";
    ui->lineEditFinanceValue->setText(QString::number(financeValue, 'f', 2));

    exec();
}

void TicketMoneyAdjustWidget::on_pushButtonCancel_clicked()
{
    this->close();
}

void TicketMoneyAdjustWidget::on_pushButtonSubmit_clicked()
{
    if(userService.inputOperatePassword(this) == false){
        return;
    }

    userRelationService.adjustUserRelationFinance(
                this,
                ui->pushButtonSubmit,
                relation,
                message->messageId,
                ticketValue,
                moneyValue);
}
